<template>
  <div id="login">
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      :rules="rules"
      status-icon
      label-width="auto"
      class="demo-ruleForm"
    >
      <div class="zhuce" @click="Register()">
        {{ isRegister ? "注册" : "登录" }}
      </div>
      <div class="denglu">{{ isRegister ? "登录系统" : "注册账号" }}</div>

      <el-form-item label="用户名:  " prop="userName">
        <el-input v-model="ruleForm.userName" style="width: 20rem" />
      </el-form-item>
      <el-form-item label="密   码:" prop="pass">
        <el-input
          v-model="ruleForm.pass"
          type="password"
          style="width: 20rem"
        />
      </el-form-item>
      <div class="btn">
        <el-button type="primary" @click="submitForm(ruleFormRef)">{{
          isRegister ? "登录" : "注册"
        }}</el-button>
      </div>
    </el-form>
  </div>
</template>
  
  <script lang="ts" setup>
import { reactive, ref } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { apiLogUsers, apiLoginUsers } from "@/utils/api";
import router from "@/router";
import { ElMessage } from "element-plus";
// 状态管理 pina  composition API模式
import { useCounterStore } from "@/stores/counter.js";
// composition API模式
const counterStore = useCounterStore();

// import { type counter } from "../stores/counter";
// const counterww = counter();

const ruleFormRef = ref<FormInstance>();
const validatePass = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请输入密码！"));
  } else {
    if (ruleForm.pass !== "") {
      if (!ruleFormRef.value) return;
      ruleFormRef.value.validateField("pass", () => null);
    }
    callback();
  }
};
const validateuserName = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请输入用户名！"));
  } else if (value !== ruleForm.userName) {
    callback(new Error("Two inputs don't match!"));
  } else {
    callback();
  }
};

const ruleForm = reactive({
  pass: "",
  userName: "",
});

const rules = reactive<FormRules<typeof ruleForm>>({
  pass: [{ validator: validatePass, trigger: "blur" }],
  userName: [{ validator: validateuserName, trigger: "blur" }],
});
// 登录

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      console.log(ruleForm, valid, "submit!");
      apiLoginUsers(ruleForm).then((res: any) => {
        if (res.data.length != 0) {
          // 浏览器储存token

          sessionStorage.setItem("token", res.data[0].token);
          sessionStorage.setItem("userName", res.data[0].userName);
          console.log(res.data[0], "res.data.userName");
          // pina储存
          counterStore.userNamePass = res.data[0];
          router.push("/jsonIndexView");
        } else {
          // 弹窗提示账号密码错误
          ElMessage.error("账号或密码错误！");
        }
      });
    } else {
      console.log("error submit!");
      return false;
    }
  });
};

// 注册
const isRegister = ref(true);
const Register = () => {
  isRegister.value = !isRegister.value;
};
</script>

<style lang="scss" scoped>
#login {
  height: 100%;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #778fc2;
  .demo-ruleForm {
    border: 2px solid #a39aa1;
    padding: 0 3rem 3rem 3rem;
    background: #badc82;
    .zhuce {
      text-align: end;
      padding-top: 10px;
      color: #fff;
    }
    .denglu {
      font-size: 32px;
      font-weight: 800;
      padding: 20px 0;
      text-align: center;
    }
    .btn {
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>
  